#ifndef ENTITY
#define ENTITY

#include "mains.h"
#include "motor.h"
#include "glob.h"
#include "input.h"

class Entity
{
    public:
        Entity();
        ~Entity();
        SDL_Texture *TextureOpt;
        SDL_Surface *TempSurface;
        SDL_Rect SrcR;
        SDL_Rect DestR;
        const std::string File;
        int SpriteID;
        int Velocity;
        int SpriteHeight;
        int SpriteWidth;
        int SpriteXOfst;
        int SpriteYOfst;
        void DestTexture();
        void DrawSprite();
        int LoadSprite(const std::string File, int SpriteID);
        int SetRects(int SpriteHeight, int SpriteWidth, int SpriteXOfst, int SpriteYOfst);
        int SetOffset(int SpriteXOfst, int SpriteYOfst);
        void HandleInput();
        void ShowMove();
};

Entity::Entity()
{
    Velocity = 0;
}

Entity::~Entity()
{
    DestTexture();
}

int Entity::LoadSprite(const std::string File, int SpriteID)
{
    TempSurface = IMG_Load(File.c_str());
    IMGErrorPrint();
    TextureOpt = SDL_CreateTextureFromSurface(GetRenderer(), TempSurface);

    if (&TempSurface != NULL) {
        SDL_FreeSurface(TempSurface);
        std::cout << "Freed surface\n";
    }

    if (&TextureOpt == NULL) {
        std::cout << "Could not load sprite\n";
        SDLErrorPrint();
        return (-1);
    }
    else if (&TextureOpt != NULL) {
        std::cout << "Sprite " << File
            << " loaded.\t" << "SpriteID:" << SpriteID << "\n";
        SDLErrorPrint();
        return (-2);
    }
    return 0;
}

int Entity::SetRects(int SpriteHeight, int SpriteWidth, int SpriteXOfst, int SpriteYOfst)
{
    SrcR.x = 0;
    SrcR.y = 0;
    SrcR.h = SpriteHeight;
    SrcR.w = SpriteWidth;
    DestR.x = SpriteXOfst;
    DestR.y = SpriteYOfst;
    DestR.h = SpriteHeight;
    DestR.w = SpriteWidth;
    return 0;
}

int Entity::SetOffset(int SpriteXOfst, int SpriteYOfst)
{
    SrcR.x = SpriteXOfst;
    SrcR.y = SpriteYOfst;
    SDLErrorPrint();
}

void Entity::DestTexture()
{
    if (TextureOpt != NULL) {
        SDL_DestroyTexture(TextureOpt);
        std::cout << "Texture " << File << " destroyed\n";
        SDLErrorPrint();
    }
    else if (TextureOpt == NULL) {
        std::cout << "No texture to destroy\n";
        SDLErrorPrint();
    }
}

void Entity::DrawSprite()
{
    SDL_RenderCopy(GetRenderer(), TextureOpt, &SrcR, &DestR);
    SDL_RenderPresent(GetRenderer());
    SDLErrorPrint();
}

void Entity::HandleInput()
{
    if (MainEvent.type == SDL_KEYDOWN) {
        switch (MainEvent.key.keysym.sym) {
            case SDLK_RIGHT: Velocity += DestR.w; break;
            case SDLK_LEFT: Velocity -= DestR.w; break;
        }
    }
    else if (MainEvent.type == SDL_KEYUP) {
        switch (MainEvent.key.keysym.sym) {
            case SDLK_RIGHT: Velocity -= DestR.w; break;
            case SDLK_LEFT: Velocity += DestR.w; break;
        }
    }
}

void Entity::ShowMove()
{
    DestR.w += Velocity;
    SDL_RenderCopy(GetRenderer(), TextureOpt, &SrcR, &DestR);
    SDL_RenderPresent(GetRenderer());
}

#endif
